package com.mall.mapper

import com.mall.entity.Product
import org.apache.ibatis.annotations.*

/**
 * 商品数据访问接口
 */
@Mapper
interface ProductMapper {
    
    /**
     * 创建商品
     */
    @Insert("INSERT INTO products (name, description, price, stock, status, created_at, updated_at) " +
            "VALUES (#{name}, #{description}, #{price}, #{stock}, #{status}, now(), now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    fun insert(product: Product): Int
    
    /**
     * 根据ID查找商品
     */
    @Select("SELECT * FROM products WHERE id = #{id} AND deleted_at IS NULL")
    fun findById(id: Long): Product?
    
    /**
     * 更新商品信息
     */
    @Update("UPDATE products SET name = #{name}, description = #{description}, " +
            "price = #{price}, stock = #{stock}, status = #{status}, updated_at = now() " +
            "WHERE id = #{id} AND deleted_at IS NULL")
    fun update(product: Product): Int
    
    /**
     * 删除商品（软删除）
     */
    @Update("UPDATE products SET deleted_at = now() WHERE id = #{id} AND deleted_at IS NULL")
    fun delete(id: Long): Int
    
    /**
     * 获取商品总数
     */
    @Select("SELECT COUNT(*) FROM products WHERE deleted_at IS NULL")
    fun count(): Long
    
    /**
     * 获取商品列表（分页）
     */
    @Select("SELECT * FROM products WHERE deleted_at IS NULL LIMIT #{offset}, #{pageSize}")
    fun findAll(@Param("offset") offset: Int, @Param("pageSize") pageSize: Int): List<Product>
}